package com.example.score.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.score.entity.Student;
import com.example.score.service.StudentService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;
/**
 * @author shenrui
 * @date 2021/9/28
 * @description 对学生的增删改查以及状态变化的操作
 */
@Controller
public class StudentController {

    @Autowired
    StudentService studentService;

    /**
     *@description 查找学生列表
     * @return String
     */
    @GetMapping("/admin_stu_list")
    public String studentManage(HttpSession session) {
        List<Student> studentList = studentService.list();
        session.setAttribute("studentList", studentList);
        return "admin_stu_list";
    }

    /**
     *@description 跳转学生修改界面
     * @return String
     */
    @GetMapping("/stu_revise_jump/{id}")
    public String reviceStudentJump(@PathVariable("id") Long id,
                                 HttpSession session) {
        Student student = studentService.getById(id);
        session.setAttribute("studentInfo",student);
        return "redirect:/revise_stu_info";
    }

    /**
     *@description 修改学生信息
     * @return String
     */
    @PostMapping("/submit_stu_info")
    public String reviceStudentInfo(Student student, HttpSession session, Model model) {
        //填入信息不能为空
        if (StringUtils.isNullOrEmpty(student.getStuName())) {
            model.addAttribute("stuMsg", "信息不能为空");
            return "revise_stu_info";

        }
        //进行更新
        Boolean bool = studentService.updateById(student);
        if (bool) {
            return "redirect:/admin_stu_list";
        } else {
            model.addAttribute("stuMsg", "修改失败");
            return "revise_stu_info";
        }

    }

    /**
     *@description 跳转密码修改界面
     * @return String
     */
    @GetMapping("/stu_password_jump/{id}")
    public String studentPasswordJump(@PathVariable("id") Long id,
                                    HttpSession session) {
        Student student = studentService.getById(id);
        session.setAttribute("studentInfo",student);
        return "redirect:/revise_stu_password";
    }

    /**
     *@description 修改密码
     * @return String
     */
    @PostMapping("/submit_stu_password")
    public String reviceStudentPassword(Student student,  Model model) {
        //填入信息不能为空
        if (StringUtils.isNullOrEmpty(student.getStuPassword())) {
            model.addAttribute("stuMsg", "密码不能为空");
            return "revise_stu_password";

        }
        BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
        String hashPass = bcryptPasswordEncoder.encode(student.getStuPassword());
        student.setStuPassword(hashPass);
        //进行更新
        Boolean bool = studentService.updateById(student);
        if (bool) {
            return "redirect:/admin_stu_list";
        } else {
            model.addAttribute("stuMsg", "修改失败");
            return "revise_stu_password";
        }

    }

    /**
     *@description 改变学生状态
     * @return String
     */
    @RequestMapping("/stu_status/{id}")
    public String studentStatus(@PathVariable("id") Long id,
                                      HttpSession session) {
        Student student = studentService.getById(id);
        if(student.getStuStatus() == 1) {
            student.setStuStatus(0);
        } else {
            student.setStuStatus(1);
        }
        studentService.updateById(student);
        return "redirect:/admin_stu_list";
    }

    /**
     *@description 添加学生
     * @return String
     */
    @PostMapping("/submit_stu_add")
    public String studentAdd(Student student, HttpSession session, Model model) {
        //填入信息不能为空
        if (StringUtils.isNullOrEmpty(student.getStuNumber()) || StringUtils.isNullOrEmpty(student.getStuPassword()) || StringUtils.isNullOrEmpty(student.getStuName()) ) {
            model.addAttribute("stuMsg", "信息不能为空");
            return "admin_stu_add";

        }
        //学号不能重复
        if (studentService.count(new LambdaQueryWrapper<Student>().eq(Student::getStuNumber, student.getStuNumber())) > 0) {
            model.addAttribute("stuMsg", "学号重复");
            return "admin_stu_add";
        }
        BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
        String hashPass = bcryptPasswordEncoder.encode(student.getStuPassword());
        student.setStuPassword(hashPass);
        //判断是否成功插入数据库
        if(studentService.save(student)) {
            return "redirect:/admin_stu_list";
        } else {
            model.addAttribute("stuMsg", "添加失败");
            return "admin_stu_add";
        }
    }
}
